package com.quan.junit;

import org.junit.Test;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class GeneratorServiceEntity {

	private String dbUrl = "jdbc:mysql://localhost:3306/quan_sport";
	private String driverClass = "com.mysql.jdbc.Driver";
	private String username = "quan_sport";
	private String password = "quan_sport";
	
	@Test
    public void generateCode() {
        String packageName = "com.quan.upms";
        generateByTables(packageName, "tb_simple", "tb_upms_user");
    }

    private void generateByTables(String packageName, String... tableNames) {
    	
    	AutoGenerator autoGenerator = new AutoGenerator();
    	
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setUrl(dbUrl);
        dataSourceConfig.setUsername(username);
        dataSourceConfig.setPassword(password);
        dataSourceConfig.setDriverName(driverClass);
        autoGenerator.setDataSource(dataSourceConfig);
        
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setTablePrefix("tb_");
        strategyConfig.setCapitalMode(true);
        strategyConfig.setEntityLombokModel(false);
        strategyConfig.setDbColumnUnderline(true);
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setInclude(tableNames);//修改替换成你需要的表名，多个表名传数组
        autoGenerator.setStrategy(strategyConfig);
        
        GlobalConfig config = new GlobalConfig();
        config.setActiveRecord(false);
        config.setAuthor("杨浩泉");
        config.setOutputDir("D:\\mars\\Workspaces\\eclipse\\SpringCloud\\Prod-Sprot\\quan-sport-project\\quan-sport-service-provider\\quan-sport-service-provider-upms-mp\\src\\main\\java");
        config.setFileOverride(true);
        config.setServiceName("%sService");
        autoGenerator.setGlobalConfig(config);
        
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent(packageName);
        packageConfig.setMapper("dao.mapper");
        packageConfig.setXml("dao.mapper");
        packageConfig.setService("service");
        packageConfig.setServiceImpl("service.impl");
        packageConfig.setController("web.controller");
        packageConfig.setEntity("entity");
        
        autoGenerator.setPackageInfo(packageConfig);
        
        autoGenerator.execute();
    }
}
